﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BoardSquare.cs" company="">
//   
// </copyright>
//  <summary>
//   The board location.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Shatranj
{
    using System;
    using System.ComponentModel;

    /// <summary>
    /// The board square.
    /// </summary>
    public class BoardSquare : INotifyPropertyChanged
    {
        #region Fields

        /// <summary>
        /// The _algebraic identity with piece.
        /// </summary>
        private string _algebraicIdentityWithPiece;

        /// <summary>
        /// The _algebric identity.
        /// </summary>
        private string _algebricIdentity;

        /// <summary>
        /// The _current piece.
        /// </summary>
        private ChessPiece _currentPiece;

        /// <summary>
        /// The _is hit.
        /// </summary>
        private bool _isHit;

        /// <summary>
        /// The location.
        /// </summary>
        private BoardLocation location;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BoardSquare"/> class.
        /// </summary>
        /// <param name="parentBoard">
        /// The parent board.
        /// </param>
        public BoardSquare(IChessboard parentBoard)
        {
            this.Board = parentBoard;
        }

        #endregion

        #region Public Events

        /// <summary>
        /// The property changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the algebraic identity.
        /// </summary>
        public string AlgebraicIdentity
        {
            get
            {
                return this._algebricIdentity;
            }

            set
            {
                this._algebricIdentity = value;
                this.Location = PositionCalculator.ParseForBoardPosition(value);
            }
        }

        /// <summary>
        /// Gets or sets the algebraic identity with piece.
        /// </summary>
        public string AlgebraicIdentityWithPiece
        {
            get
            {
                return this._algebraicIdentityWithPiece;
            }

            set
            {
                this._algebraicIdentityWithPiece = value;
                this.OnPropertyChanged("AlgebraicIdentityWithPiece");
            }
        }

        /// <summary>
        /// Gets or sets the board.
        /// </summary>
        public IChessboard Board { get; set; }

        /// <summary>
        /// Gets the center.
        /// </summary>
        public BoardSquare Center
        {
            get
            {
                return this;
            }
        }

        /// <summary>
        /// Gets or sets the current piece.
        /// </summary>
        public ChessPiece CurrentPiece
        {
            get
            {
                return this._currentPiece;
            }

            set
            {
                this._currentPiece = value;
                this.OnPropertyChanged("CurrentPiece");
            }
        }

        /// <summary>
        /// Gets or sets the east.
        /// </summary>
        public BoardSquare East { get; set; }

        /// <summary>
        /// Gets or sets the file.
        /// </summary>
        public BoardFile File { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// Gets a value indicating whether is empty.
        /// </summary>
        public bool IsEmpty
        {
            get
            {
                return this.CurrentPiece == null;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether is hit.
        /// </summary>
        public bool IsHit
        {
            get
            {
                return this._isHit;
            }

            set
            {
                this._isHit = value;
                this.OnPropertyChanged("IsHit");
            }
        }

        /// <summary>
        /// Gets the location.
        /// </summary>
        public BoardLocation Location
        {
            get
            {
                Console.WriteLine("X={0}, Y={1}", this.location.X, this.location.Y);
                return this.location;
            }

            private set
            {
                this.location = value;
                this.OnPropertyChanged("Location");
            }
        }

        /// <summary>
        /// Gets or sets the north.
        /// </summary>
        public BoardSquare North { get; set; }

        /// <summary>
        /// Gets or sets the northeast.
        /// </summary>
        public BoardSquare Northeast { get; set; }

        /// <summary>
        /// Gets or sets the northwest.
        /// </summary>
        public BoardSquare Northwest { get; set; }

        /// <summary>
        /// Gets or sets the notation.
        /// </summary>
        public string Notation { get; set; }

        /// <summary>
        /// Gets or sets the south.
        /// </summary>
        public BoardSquare South { get; set; }

        /// <summary>
        /// Gets or sets the southeast.
        /// </summary>
        public BoardSquare Southeast { get; set; }

        /// <summary>
        /// Gets or sets the southwest.
        /// </summary>
        public BoardSquare Southwest { get; set; }

        /// <summary>
        /// Gets or sets the west.
        /// </summary>
        public BoardSquare West { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The select.
        /// </summary>
        /// <param name="showHitHints">
        /// The show hit hints.
        /// </param>
        /// <param name="moveOptions">
        /// The move options.
        /// </param>
        public void Select(bool showHitHints, MoveOptions moveOptions)
        {
            var squares = MoveCalculator.Calculate(this.CurrentPiece, this, moveOptions);
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on property changed.
        /// </summary>
        /// <param name="propertyName">
        /// The property name.
        /// </param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}